﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.Product;

namespace ZBJF.Warranty.BLL
{
    public partial class AccessoriesLevelService
    {
        #region 根据主键ID获取配件等级 + AccessoriesLevel LoadByID(int id)
        public AccessoriesLevel LoadByID(int id)
        {
            return LoadEntities(a => a.ID == id).FirstOrDefault();
        }
        #endregion

        #region 判断配件等级是否存在 + bool IsExist(long productTypeID, int accessoriesTypeID, string accessoriesLevelName)

        /// <summary>
        /// 判断配件等级是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool IsExist(long productTypeID, int accessoriesTypeID, string accessoriesLevelName,int id=0)
        {
            var expressionWhere = PredicateExtensions.True<AccessoriesLevel>();
            if (id > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ID != id);
            }
            expressionWhere = expressionWhere.And(o => o.ProductTypeID==productTypeID);
            expressionWhere = expressionWhere.And(o => o.AccessoriesTypeID==accessoriesTypeID);
            expressionWhere = expressionWhere.And(o => o.AccessoriesLevelName.Equals(accessoriesLevelName));
            return LoadEntities(expressionWhere).Any();
        }
        #endregion

        #region 分页获取配件等级列表 + IQueryable<AccessoriesLevel> GetPagingList(AccessoriesLevelModel model, out int total)
        /// <summary>
        /// 分页获取配件等级列表
        /// </summary>
        /// <param name="model">过滤条件</param>
        /// <param name="total">总数量</param>
        /// <returns></returns>

        public IQueryable<AccessoriesLevel> GetPagingList(AccessoriesLevelModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<AccessoriesLevel>();

            //设备类型名称
            if (model.ProductTypeID > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ProductTypeID == model.ProductTypeID);
            }
            //配件类型ID
            if (model.AccessoriesTypeID > 0)
            {
                expressionWhere = expressionWhere.And(o => o.AccessoriesTypeID == model.AccessoriesTypeID);
            }

            //配件等级名称
            if (!string.IsNullOrEmpty(model.AccessoriesLevelName))
            {
                expressionWhere = expressionWhere.And(o => o.AccessoriesLevelName.Contains(model.AccessoriesLevelName));
            }

            if (string.IsNullOrWhiteSpace(model.iSorts))
            {
                model.iSorts = "ID DESC";
            }
            return LoadPagerEntities<AccessoriesLevel>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }
        #endregion

        public IQueryable<AccessoriesLevel> GetAccessoriesLevelByAccessoriesTypeId(long accessoriesTypeId)
        {
            return LoadEntities(n => n.AccessoriesTypeID == accessoriesTypeId);
        }
      
    }
}
